import { Scene } from '@babylonjs/core/scene';
import { AbstractControl, IAbstractControlOptions } from './abstract-control';
import { Control } from '@babylonjs/gui/2D/controls/control';
import { Image } from '@babylonjs/gui/2D/controls/image';


/**
 * 图片控件参数对象
 * @param url 图片地址
 * @param stretch 图片拉伸模式
 */
interface IUIImageOptions extends IAbstractControlOptions {
    url?: string,
    stretch?: number,
}


/**
 * ui图片控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 */
class UIImage extends AbstractControl {

    private _url!: string | null;

    constructor(name: string, scene: Scene, options?: IUIImageOptions) {
        super(name, scene, options);
    }

    /**
     * 设置图片地址
     */
    public set url(url: string | null) {
        this._url = url;
        (this.control as Image).source = url;
    }

    /**
     * 获取图片地址
     */
    public get url(): string | null {
        return (this.control as Image).source;
    }

    protected initializeControl(name: string, scene: Scene, options: IUIImageOptions): Image {
        const img = new Image(name + '-image');
        this._url = options.url || null;
        img.source = this._url;
        img.stretch = options.stretch === undefined ? Image.STRETCH_FILL : options.stretch;
        return img;
    }

    protected bindEvent(control: Control, options: IUIImageOptions): void {
    }

}


/**
 * 创建ui图片控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 * @returns 图片控件
 */
const CreateUIImage = (name: string, scene: Scene, options?: IUIImageOptions): UIImage => {
    const img = new UIImage(name, scene, options);
    return img;
};


export { UIImage, CreateUIImage };
export type { IUIImageOptions };